package CuratorCtrl;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;
import javax.swing.BoxLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ManageInterface extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jPanel1;
	private JButton bu_Run;
	private JButton bu_Edit;
	private JButton bu_network;
	private JCheckBox ckb_hide;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private JList jList_VMList;
	private JPanel jPanel2;
	private String[] namelist;
	public static Vector<String> imagelist=new Vector<String>();
	private boolean running=false;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ManageInterface inst = new ManageInterface();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ManageInterface() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GridLayout thisLayout = new GridLayout(1, 1);
			thisLayout.setColumns(1);
			thisLayout.setHgap(5);
			thisLayout.setVgap(5);
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{	
			jPanel1 = new JPanel();
			getContentPane().add(jPanel1);
			{	addlist();
			
				
			ListModel jList1Model = 
					new DefaultComboBoxModel(namelist);
			jList_VMList = new JList();
			jPanel1.add(jList_VMList);
			jList_VMList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jList_VMList.setModel(jList1Model);
			jList_VMList.setDragEnabled(true);
			jList_VMList.setSize(180, 260);
			jList_VMList.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			jList_VMList.setFixedCellHeight(20);
			jList_VMList.setFixedCellWidth(180);
			//jList_VMList.setDefaultLocale(new java.util.Locale("en"));
			jList_VMList.setPreferredSize(new java.awt.Dimension(180, 260));
			jList_VMList.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent evt) {
					try {
						jList_VMListValueChanged(evt);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			}
			}
			{
				jPanel2 = new JPanel();
				BoxLayout jPanel2Layout = new BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS);
				jPanel2.setLayout(jPanel2Layout);
				getContentPane().add(jPanel2);
				{
					jPanel3 = new JPanel();
					jPanel2.add(jPanel3);
					{
						bu_Run = new JButton();
						jPanel3.add(bu_Run);
						bu_Run.setText("Run");
						bu_Run.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								try {
									bu_RunActionPerformed(evt);
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}
					{
						bu_Edit = new JButton();
						jPanel3.add(bu_Edit);
						bu_Edit.setText("Edit");
						bu_Edit.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								bu_EditActionPerformed(evt);
							}
						});
					}
					{
						bu_network = new JButton();
						jPanel3.add(bu_network);
						bu_network.setText("Network Configure");
						bu_network.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								try {
									bu_networkActionPerformed(evt);
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}
				}
				{
					jPanel4 = new JPanel();
					jPanel2.add(jPanel4);
					{
						ckb_hide = new JCheckBox();
						jPanel4.add(ckb_hide);
						ckb_hide.setText("Background Mode");
						ckb_hide.setSelected(true);
					}
				}
			}
		//	imagelist=Util.readfromcsv();
			pack();
			setSize(400, 300);
			
			
			

		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	public void addlist() throws IOException{
		imagelist=Util.read_VMlist();
		if(imagelist.size()==0){
			namelist=new String[1];
			namelist[0]="No Image in current host";
			return;
		}else{
			namelist=new String[imagelist.size()];
			imagelist.toArray(namelist);

			
			
		}		
		
		
		}
	
	private void bu_RunActionPerformed(ActionEvent evt) throws IOException {
		System.out.println("bu_Run.actionPerformed, event="+evt);
		int idx=jList_VMList.getSelectedIndex();
		if(idx==-1){
			JOptionPane.showMessageDialog(null,"please select one vm");
			return;
		}
		Vector<String> runninglist=Util.read_runninglist();
		running=runninglist.contains(imagelist.get(idx));
		
		if(!running)
		Util.runVM(imagelist.get(idx), ckb_hide.isSelected());
		else
			Util.closeVM(imagelist.get(idx));
		Vector<String> new_runninglist=Util.read_runninglist();
		running=new_runninglist.contains(imagelist.get(idx));

		if(running)bu_Run.setText("Poweroff");
		else
			bu_Run.setText("Run");
		
		
		//TODO add your code for bu_Run.actionPerformed
	}
	
	private void bu_EditActionPerformed(ActionEvent evt) {
		System.out.println("bu_Edit.actionPerformed, event="+evt);
		int idx=jList_VMList.getSelectedIndex();
		if(idx==-1){
			JOptionPane.showMessageDialog(null,"please select one vm");
			return;
		}
		EditFrame ef=new EditFrame(imagelist.get(idx));
		//TODO add your code for bu_import.actionPerformed
		ef.setLocationRelativeTo(null);
		
		ef.setVisible(true);
		//TODO add your code for bu_Edit.actionPerformed
	}
	
	private void jList_VMListValueChanged(ListSelectionEvent evt) throws IOException {
		System.out.println("jList_VMList.valueChanged, event="+evt);
		int idx=jList_VMList.getSelectedIndex();
		//boolean found=false;
		Vector<String> runninglist=Util.read_runninglist();
		running=runninglist.contains(imagelist.get(idx));
		if(running)bu_Run.setText("Poweroff");
		else
			bu_Run.setText("Run");
		
		//TODO add your code for jList_VMList.valueChanged
	}
	
	private void bu_networkActionPerformed(ActionEvent evt) throws IOException {
		System.out.println("bu_network.actionPerformed, event="+evt);
		int idx=jList_VMList.getSelectedIndex();
		if(idx==-1){
			JOptionPane.showMessageDialog(null,"please select one vm");
			return;
		}
		String name=namelist[idx];
		PortManage pm=new PortManage(name);
		
		pm.setLocationRelativeTo(null);
		pm.setVisible(true);	
		
		//TODO add your code for bu_network.actionPerformed
	}

}
